package com.tchintech.pay.view.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;

import com.tchintech.pay.R;
import com.tchintech.pay.adapter.GeneralizeAdapter;
import com.tchintech.pay.delegate.GeneralizeDeleagte;
import com.tchintech.pay.frame.presenter.ActivityPresenter;
import com.tchintech.pay.model.entity.Generalize;
import com.tchintech.pay.model.entity.HttpCode;
import com.tchintech.pay.net.OkhttpClientTest;
import com.tchintech.pay.utils.AppConst;
import com.tchintech.pay.view.fragment.MainFragment;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/18 0018.
 * <p>
 * 费率详情
 */

public class GeneralizeActivity extends ActivityPresenter<GeneralizeDeleagte> implements View.OnClickListener {
    private GeneralizeAdapter generalizeAdapter;
    private List<Generalize> list;
    private ListView listView;
    private Map<String, String> map;
    private String cardUrl = "FastPayment.quick";
    private String gardes_url = "Grades.gold";
    private String rate_url = "Grades.rate";
    private Handler handler;
    private Intent intent;

    @Override
    protected void initView() {
        super.initView();
        listView = viewDelegate.getLv();
        viewDelegate.setTv_title("费率信息");
        viewDelegate.setOnClickListener(this, R.id.iv_back);
        generalizeAdapter = new GeneralizeAdapter(this, list);
        listView.setAdapter(generalizeAdapter);
        handler();
        generalizeAdapter.setOnitemDataClick(new GeneralizeAdapter.OnitemDataClick() {
            @Override
            public void OnItemDataClick(Generalize generalize) {
                getGold();
            }
        });
        MainFragment.listA.add(this);
        getRate();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    String card = data.getStringExtra("card_num");
                    submit(card);
                }
                break;
        }
    }

    /**
     * 获取当前的费率
     */
    private void getRate() {
        map = new HashMap<String, String>();
        showProgressDialog("", "加载中，请稍后...");
        new OkhttpClientTest().path(this, handler, rate_url, map, HttpCode.RARES);
    }

    /**
     * 查询当前登录人是否已经发展了3个黄金会员
     */
    private void getGold() {
        map = new HashMap<String, String>();
        map.put("phone", AppConst.getUser().getPhone());
        showProgressDialog("", "加载中，请稍后...");
        new OkhttpClientTest().path(this, handler, gardes_url, map, HttpCode.GRADES_GOLD);

    }


    /**
     * 银联快捷支付
     *
     * @param card
     */
    private void submit(String card) {
        map = new HashMap<String, String>();
        map.put("user_id", AppConst.getUser().getId());
        map.put("amount", "100.00");
        map.put("paybank", card);
        map.put("des", "升级");
        map.put("Channel_type", "MLZF");
        showProgressDialog("", "加载中，请稍后...");
        new OkhttpClientTest().path(this, handler, cardUrl, map, HttpCode.QUICKPAY);
    }

    private void handler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                hideProgressDialog();
                switch (msg.what) {
                    case HttpCode.networkabnormal:
                        showMsg("当前网络不可用");
                        break;
                    case HttpCode.http_failed:
                        showMsg("连接失败");
                        break;
                    case HttpCode.http_timeout:
                        showMsg("请求超时");
                        break;
                    case HttpCode.RARES:
                        try {
                            JSONObject json = new JSONObject(msg.obj.toString());
                            if (json.getString("ret").equals("1")) {
                                String[] wx = new String[4];
                                String[] quick = new String[4];
                                JSONArray jsonArray = json.getJSONArray("data");
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                                    String dQuick = jsonObject.getString("ratevalue");
                                    dQuick = dQuick.substring(0, 4);
                                    double other = jsonObject.getDouble("online_wx_d0");
                                    other = other * 100;
                                    quick[i] = dQuick + "%";
                                    wx[i] = other + "%";
                                    Generalize generalize = new Generalize();
                                    generalize.setGrade(i + 1);
                                    generalize.setWx(wx[i]);
                                    generalize.setZfb(wx[i]);
                                    generalize.setQuick(quick[i]);
                                    list.add(generalize);
                                }
                                generalizeAdapter.setList(list);
                            } else {
                                showMsg("费率查询失败");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                    case HttpCode.GRADES_GOLD:
                        try {
                            JSONObject json = new JSONObject(msg.obj.toString());
                            if (json.getString("ret").equals("1")) {
                                //快捷支付2
                                intent.setClass(GeneralizeActivity.this, CardActivity.class);
                                intent.putExtra("state", "2");
                                intent.putExtra("channel_name", "MLZF");
                                startActivityForResult(intent, 1);
                            } else {
                                showMsg(json.getString("msg"));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                    case HttpCode.QUICKPAY:
                        try {
                            JSONObject json = new JSONObject(msg.obj.toString());
                            if (json.getString("ret").equals("1")) {
                                System.out.println("快捷支付" + json);
                                intent.putExtra("data", json.getString("data"));
                                intent.setClass(GeneralizeActivity.this, QuickPayThreeActivity.class);
                                startActivity(intent);
                            } else {
                                showMsg("数据提交失败");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                }
            }
        };
    }


    @Override
    protected void initData() {
        super.initData();
        intent = new Intent();
        list = new ArrayList<Generalize>();
        //微信和支付宝的费率一样所以这里中放了一个

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }

    }

    @Override
    protected Class<GeneralizeDeleagte> getDelegateClass() {
        return GeneralizeDeleagte.class;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MainFragment.listA.clear();
    }
}
